﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using TravelSite.Entities;
using TravelSite.DAL;
using TravelSite.DAL.Repositories;
using Travel.Api.Code;

namespace Travel.Api.Controllers
{
    public class TravelPackageController : ApiController
    {
        [HttpGet]
        [RequireHttps]
        public List<TravelPackage> GetList(string pin)
        {
            Contact cnt = Utils.GetContact(pin);
            if (cnt == null)
                throw new HttpResponseException(ErrorUtils.GetErrorResponse(ErrorCodes.NotValidPin));

            using (var repo = RepositoryFactory.ResolveRepository<ApiTravelPackageRepository>())
            {
                return repo.FindByClient(cnt.ContactId).ToList();
            }
        }

        [HttpGet]
        [RequireHttps]
        public List<PackageDestinationOption> GetOptions(int ID, string pin, int travelId)
        {
            Contact cnt = Utils.GetContact(pin);
            if (cnt == null)
                throw new HttpResponseException(ErrorUtils.GetErrorResponse(ErrorCodes.NotValidPin));

            using (var repo = RepositoryFactory.ResolveRepository<ApiTravelPackageRepository>())
            {
                return repo.FindOptions(ID, travelId).ToList();
            }
        }

        /*[HttpPost]
        public HttpResponseMessage SetPkgDest(int travelId, string pin, int pkgDestId)
        {
            Contact cnt = Utils.GetContact(pin);
            if (cnt == null)
                throw new HttpResponseException(ErrorUtils.GetErrorResponse(ErrorCodes.NotValidPin));
            
            using (var repo = RepositoryFactory.ResolveRepository<ApiTravelPackageRepository>())
            {
                int result = repo.SetPkgDest(travelId, cnt.ContactId, pkgDestId);
                if (result > 0)
                    throw new HttpResponseException(ErrorUtils.GetErrorResponse((ErrorCodes)result));
                else
                {
                    return new HttpResponseMessage(HttpStatusCode.NoContent);
                }
            }
 
        }*/

        [HttpPost]
        [RequireHttps]
        public HttpResponseMessage SetPkgChoice(int travelId, string pin, int pkgDestId, string choiceIdList, DateTime startDate)
        {
            Contact cnt = Utils.GetContact(pin);
            if (cnt == null)
                throw new HttpResponseException(ErrorUtils.GetErrorResponse(ErrorCodes.NotValidPin));

            using (var repo = RepositoryFactory.ResolveRepository<ApiTravelPackageRepository>())
            {
                int result = repo.SetPkgChoice(travelId, cnt.ContactId, pkgDestId, choiceIdList, startDate);
                if (result > 0)
                    throw new HttpResponseException(ErrorUtils.GetErrorResponse((ErrorCodes)result));
                else
                {
                    return new HttpResponseMessage(HttpStatusCode.NoContent);
                }
            }

        }
    }
}
